package com.qf.service.impl;

import com.qf.service.AddressService;
import com.qf.dao.AddressDao;
import com.qf.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import com.qf.utils.DataSourceUtils;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressDao dao;
    @Override
    public void addAddress(Address address) {
        dao.addAddress(address);
    }

    @Override
    public List<Address> getAddressList(int uid) {
        return dao.getAddressList(uid);
    }

    @Override
    public void updateAddress(Address address) {
        dao.updateAddress(address);
    }

    @Override
    public void deleteAddress(int id) {
        dao.deleteAddress(id);
    }

    @Override
    public void defaultAddress(int id, int aid) {
        Connection conn = DataSourceUtils.getConnection();
        try {
            //设置为不能自动提交
            conn.setAutoCommit(false);
            //把所有的收货地址都设置为 普通的
            dao.set2Normal(id);
            //设置某收货地址为 默认收货地址
            dao.defaultAddress(aid);
            //提交事务
            conn.commit();
        }catch (Exception e ){
            e.printStackTrace();
            //回滚事务
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

}
